package com.meepo.feed.history

import android.os.Bundle
import com.meepo.basic.components.listfragment.ListFragmentParams
import com.meepo.basic.schema.Enums
import com.meepo.basic.schema.common.PageResult
import com.meepo.basic.schema.common.ResponseWrapper
import com.meepo.basic.schema.feed.Item
import com.meepo.feed.API
import com.meepo.feed.Func
import com.meepo.feed.feed.FeedListFragment
import com.meepo.sdk.component.observer.ObserverManager
import com.xiaojinzi.component.support.ParameterSupport
import io.reactivex.Observable

class HistoryListFragment : FeedListFragment() {
    companion object {
        fun instance(
            action: String = "",
            listFragmentParams: ListFragmentParams = ListFragmentParams()
        ): HistoryListFragment {

            val rsp = HistoryListFragment()

            val bundle = Bundle()
            bundle.putString("action", action)
            bundle.putSerializable("listFragmentParams", listFragmentParams)

            rsp.arguments = bundle

            return rsp
        }
    }


    override fun loadDataObservable(page: Int): Observable<ResponseWrapper<PageResult<Item>>> {

        val action = ParameterSupport.getString(arguments, "action") ?: ""

        if (action == Enums.ACTION_TYPE_READ && page == 1) {
            return ObserverManager.applyFunc { Func.getReadHistoryV2() }
        }

        return API.instance().fetchFeedItemsV2(action = action, page = page)
    }
}